package com.superdry.fragment;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.drysurper.R;
import com.superdry.activity.AddressActivity;
import com.superdry.activity.BalanceActivity;
import com.superdry.activity.CouponActivity;
import com.superdry.activity.MoreActivity;
import com.superdry.activity.PlaceOrderActivity;
import com.superdry.activity.RegisterActivity;
import com.superdry.json.Userinfo;
import com.superdry.utils.SPHelper;
import com.superdry.utils.ShareHelper;

public class Fragment_Me extends Fragment implements android.view.View.OnClickListener {

	View balance;
	View coupon;
	View credits;
	View invite;
	View address;
	View us;
	View more;
	View dialog_share;
	View settingLayout;
	ImageView share_wechat;
	ImageView share_firends;
	ImageView share_qq;
	TextView share_cancel;
	PopupWindow pop;
	ImageView launcher;
	Userinfo userInfo;
	TextView userTel;
	TextView balance_tv;
	TextView credits_tv;
	Context mContext;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		settingLayout = inflater.inflate(R.layout.fragment_me, container, false);
		mContext = getActivity().getApplicationContext();

		initData();
		initView(settingLayout);

		return settingLayout;
	}

	public void initView(View v) {

		balance = v.findViewById(R.id.wo_balance);
		coupon = v.findViewById(R.id.wo_coupon);
		credits = v.findViewById(R.id.wo_credits);
		invite = v.findViewById(R.id.wo_invite_friend);
		address = v.findViewById(R.id.wo_address);
		us = v.findViewById(R.id.wo_us);
		more = v.findViewById(R.id.wo_more);
		userTel = (TextView) v.findViewById(R.id.userTel);
		balance_tv = (TextView) v.findViewById(R.id.balance_tv);
		credits_tv = (TextView) v.findViewById(R.id.credit_tv);

		// 初始化share_dialog
		dialog_share = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_share, null);
		share_wechat = (ImageView) dialog_share.findViewById(R.id.share_wechat);
		share_firends = (ImageView) dialog_share.findViewById(R.id.share_friends);
		share_qq = (ImageView) dialog_share.findViewById(R.id.share_qq);
		share_cancel = (TextView) dialog_share.findViewById(R.id.share_cancel);

		// 在sp中获取数据进行填充
		userInfo = SPHelper.getUserInfo(mContext);
		if (null != userInfo) {

			userTel.setText(userInfo.getPhoneNumber() + "");
			balance_tv.setText(userInfo.getAccount() + "");
			credits_tv.setText(userInfo.getIntegral() + "");// 积分
		} else {

			userTel.setText("未登录");
			balance_tv.setText("￥0.0");
			credits_tv.setText("0");// 积分
		}

		pop = new PopupWindow(dialog_share, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		pop.setFocusable(true);
		pop.setBackgroundDrawable(new BitmapDrawable());// 让pop自动消失

		balance.setOnClickListener(this);
		coupon.setOnClickListener(this);
		credits.setOnClickListener(this);
		invite.setOnClickListener(this);
		address.setOnClickListener(this);
		us.setOnClickListener(this);
		more.setOnClickListener(this);
		share_wechat.setOnClickListener(this);
		share_firends.setOnClickListener(this);
		share_qq.setOnClickListener(this);
		share_cancel.setOnClickListener(this);
	}

	public void initData() {

		userInfo = SPHelper.getUserInfo(getActivity());
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == 1) {
			if (data != null) {
				double account = data.getDoubleExtra("money", 0) + userInfo.getAccount();
				balance_tv.setText("￥" + account);
				userInfo.setAccount(account);
				SPHelper.putString(getActivity(), SPHelper.USERINFO, JSON.toJSONString(userInfo));
			}
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.wo_balance:
			Intent intent = new Intent(getActivity(), BalanceActivity.class);
			startActivityForResult(intent, 1);
			break;
		case R.id.wo_coupon:
			click2NextAty(CouponActivity.class);
			break;
		case R.id.wo_credits:

			break;
		case R.id.wo_invite_friend:
			pop.showAtLocation(settingLayout, Gravity.BOTTOM, 0, 0);
			break;
		case R.id.wo_address:

			click2NextAty(AddressActivity.class);
			break;
		case R.id.wo_us:

			break;
		case R.id.wo_more:

			click2NextAty(MoreActivity.class);
			break;

		case R.id.share_wechat:
			ShareHelper.shareToWechat(getActivity(), null);
			break;
		case R.id.share_friends:
			ShareHelper.shareToTimeLine(getActivity(), null);
			break;
		case R.id.share_qq:
			ShareHelper.shareToQQFriend(getActivity(), null);
			break;
		case R.id.share_cancel:

			pop.dismiss();
			break;
		default:
			break;
		}
	}

	public void click2NextAty(Class clazz) {

		Intent intent = new Intent();
		intent.setClass(mContext, clazz);
		startActivity(intent);
	}

}
